<style>

</style>

<template>
    <p>
        <a-space>
            <a-button type="primary" @click="onAdd">新增</a-button>
            <a-button type="primary" @click="handleQuery()">刷新</a-button>
        </a-space>
    </p>
    <a-table 
        :dataSource="passengers" 
        :columns="columns" 
        :pagination="pagination" 
        :loading="loading"
        @change="handleTableChange" 
    >
        <template #bodyCell="{ column, record }">
            <template v-if="column.dataIndex === 'operation'">
                <a-space>
                    <a-popconfirm
                        title="删除后不可恢复，确认删除?"
                        @confirm="onDelete(record)"
                        ok-text="确认"
                        cancel-text="取消"
                    >
                        <a style="color: red">删除</a>
                    </a-popconfirm>
                
                    <a @click="onEdit(record)">编辑</a>
                </a-space>
            </template>
        </template>
    </a-table>
    <a-modal v-model:visible="visible" title="乘车人" @ok="handleOk" ok-text="确认" cancel-text="取消">
        <a-form :model="passenger" :label-col="{span: 4}" :wrapper-col="{span: 14}">
            <a-form-item label="姓名">
                <a-input v-model:value="passenger.name"/>
            </a-form-item>
            <a-form-item label="身份证">
                <a-input v-model:value="passenger.idCard"/>
            </a-form-item>
            <a-form-item label="类型">
                <a-select v-model:value="passenger.type">
                    <a-select-option v-for="(item,index) in passengerTypeEnums" :key="index + 1" >{{item.desc}}</a-select-option>
                    <!-- <a-select-option value="1">成人</a-select-option>
                    <a-select-option value="2">儿童</a-select-option>
                    <a-select-option value="3">学生</a-select-option> -->
                </a-select>
            </a-form-item>
        </a-form>
    </a-modal>
</template>

<script>
import { defineComponent,ref,onMounted} from 'vue';
import { notification } from 'ant-design-vue';
import axios from 'axios';

export default defineComponent({
  name: "passenger-view",
  setup() {
    // 展示模态窗的标签的定义
    const visible = ref(false);
    const loading = ref(false);
    const addOrEdit = ref(false);

    // 定义乘车人对象
    let passenger = ref({
        id: undefined,
        numberId: undefined,
        name: undefined,
        idCard: undefined,
        type: undefined,
        createTime: undefined,
        updateTime: undefined
    });

    let passengerTypeEnums = ref([])

    const passengers = ref([]);
    
    const columns = [
        {
            title: 'id',
            dataIndex: 'id',
            key: 'id',
        },
        {
            title: '姓名',
            dataIndex: 'name',
            key: 'name',
        },
        {
            title: '身份证',
            dataIndex: 'idCard',
            key: 'idCard',
        },
        {
            title: '类型',
            dataIndex: 'type',
            key: 'type',
        },
        {
            title: '操作',
            dataIndex: 'operation',
            key: 'operation',
        },
    ];

    let pagination =ref({
        total: 0,
        current: 1,
        pageSize: 10,
    });

    // 点击新增展示模态窗的函数方法
    const onAdd = () => {
        console.log("add")
        visible.value = true;
        passenger.value = {}
        addOrEdit.value = false
    };

    // 点击编辑展示模态窗的函数方法
    const onEdit = (record) => {
        console.log("edit")
        passenger.value = window.Tool.copy(record)
        visible.value = true;
        addOrEdit.value = true
    };

     // 点击删除按钮的函数方法
    const onDelete = (record) => {
        axios.delete("/member/passenger/delete/"+record.id)
        .then( response => {
            console.log(response.data.data)
            if(response.data.data){
                notification.success({ description: '删除成功' });
                // 刷新页面
                handleQuery()
            }
        })
        .catch( error => {
            notification.error({ description:  error.response.data.data});
        })
    }

    // 点击模态窗的ok按钮执行的函数方法
    const handleOk = e => {
        // 先对type进行类型转换
        switch (passenger.value.type) {
            case "成人":
                passenger.value.type = "1"
                break;
            case "儿童":
                passenger.value.type = "2"
                break;
            case "学生":
                passenger.value.type = "3"
                break;
            default:
                break;
        }
        axios.post("/member/passenger/save", passenger.value)
            .then( response => {
                let respData = response.data
                console.log(respData)
                if(respData.data){
                    notification.success({ description: '保存成功！' });
                    // 设置为false，关闭模态窗
                    visible.value = false;
                    // 调用列表查询的接口，刷新数据
                    handleQuery()
                }else{
                    notification.error({ description: respData.errorMessage });
                }
            })
            .catch( error => {
                let errData = error.response.data
                console.log(errData)
                notification.error({ description: errData.data });
            })
    };

    // 乘车人信息列表查询的方法
    const handleQuery = e => {
        // 刷新时候不带参数的判断
        if(!e){
            e = {
                page: 1,
                limit: pagination.value.pageSize
            }
        }
        // 开启loading效果
        loading.value = true
        axios.get("/member/passenger/query",
            {
                params:{
                    page: e.page,
                    limit: e.limit
                }
            })
            .then( response => {
                // 关闭loading效果
                loading.value = false
                let respData = response.data
                let passengerDataList = respData.data.dataList
                let total = respData.data.total
                let currentPage = respData.data.currentPage
                let pageSize = respData.data.pageSize
                console.log(respData.data.dataList)
                convertType(passengerDataList)
                passengers.value = passengerDataList
                // 分页控件的设置
                pagination.value.current = currentPage
                pagination.value.pageSize = pageSize
                pagination.value.total = total
            })
            .catch( error => {
                // 关闭loading效果
                loading.value = false
                let errData = error.response.data
                console.log(errData)
                notification.error({ description: errData.data });
            })
    };

    // 转换type为文本类型
    const convertType = (passengerDataList) => {
        passengerDataList.forEach(element => {
            switch (element.type) {
            case "1":
                element.type = "成人"
                break;
            case "2":
                element.type = "儿童"
                break;
            case "3":
                element.type = "学生"
                break;
            default:
                element.type = "其他"
                break;
            }
        });
    }

    // 页码的点击事件
    const handleTableChange = (pagination) => {
        console.log("pagination:",pagination.value)
        // 调用乘车人列表查询方法
        handleQuery({
            page: pagination.current,
            limit: pagination.pageSize
        })
    };

     // PassengerTypeEnums的查询事件
     const queryPassengerTypeEnums = () => {
        console.log("查询PassengerTypeEnums的枚举数据")
        axios.get("/member/passenger/enums")
        .then( response => {
            passengerTypeEnums.value = response.data.data
            console.log(passengerTypeEnums)
        })
    };
    
    // 钩子函数，页面初始化的时候执行
    onMounted(() => {
        handleQuery({
            page: pagination.value.current,
            limit: pagination.value.pageSize
        })
        queryPassengerTypeEnums()
    });


    return {
        visible,
        loading,
        passenger,
        passengers,
        columns,
        pagination,
        passengerTypeEnums,
        onAdd,
        onEdit,
        onDelete,
        handleOk,
        handleQuery,
        handleTableChange
    };
  },
});
</script>